package club.mzywucai.blog.air_portal.pojo.domain;

import club.mzywucai.blog.air_portal.service.ScheduleService;
import club.mzywucai.blog.air_portal.util.MyStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mzywucai
 * @Description 主要解决六位数字码存储避重的问题。
 * @date 2019/5/24
 */
@Component
public class FileCodeContainer {
    @Autowired
    ScheduleService scheduleService;

    private List<String> codeList = new ArrayList<>();
    /**
     * @Description 每两个小时调度查询一次新的CodeList
     * @author mzywucai
     * @time 2019-05-24
     * @see 
     * @return   void
     * @params  []
     */
    @Scheduled(cron = "0 0 0/2 * * ? ") // 每两小时执行一次
    public void getNewFileCodeList() {
        // 获取所有的fileCode
        codeList = scheduleService.getFileCodeList();

        // 顺便删除删除过期文件
        scheduleService.delFiles();
    }

    public boolean hasContainCode(String code) {
        return this.codeList.contains(code);
    }

    public boolean addCode(String code) {
        boolean isNumber = MyStringUtil.isDigit(code);

        if (isNumber) {
            this.codeList.add(code);
        }
        return isNumber;
    }

    public boolean removeCode(String code) {
        boolean isRemove = this.codeList.remove(code);

        return isRemove;
    }
}
